package com.sfx.admin.common.biz.user;


import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UserContext {

    private static final ThreadLocal<UserInfo> USER_THREAD_LOCAL = new TransmittableThreadLocal<>();

    public static UserInfo getUserInfo() {
        return USER_THREAD_LOCAL.get();
    }

    public static void setUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            log.error("[UserContext] userInfo is null");
            return;
        }
        USER_THREAD_LOCAL.set(userInfo);
    }

    public static String getUsername() {
        UserInfo userInfo = getUserInfo();
        if (userInfo == null) {
            log.error("[UserContext] userInfo is null");
            return null;
        }
        return userInfo.getUsername();
    }

    public static String getRealName() {
        UserInfo userInfo = getUserInfo();
        if (userInfo == null) {
            log.error("[UserContext] userInfo is null");
            return null;
        }
        return userInfo.getRealName();
    }

    public static void removeUser() {
        USER_THREAD_LOCAL.remove();
    }
}